package com.example.myretrofit2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myretrofit2.MainActivity;
import com.example.myretrofit2.R;
import com.example.myretrofit2.User;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.presenter.MyPresenter;
import com.example.myretrofit2.presenter.MySubscriber;

public class LoginActivity extends AppCompatActivity {
    EditText loginName;
    EditText loginPass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logiin);
        loginName = findViewById(R.id.login_name);
        loginPass = findViewById(R.id.login_pass);
    }
    public void login(View view){
        String userName = loginName.getText().toString();
        String userPass = loginPass.getText().toString();
        if(TextUtils.isEmpty(userName)||TextUtils.isEmpty(userPass)){
            Toast.makeText(this,"用户名或密码不为空",Toast.LENGTH_SHORT).show();
        }else {
            User user = new User();
            user.setLoginPass(userPass);
            user.setLoginId(userName);
            MyPresenter.login(user, new MySubscriber<User>(this) {
                @Override
                public void onNext(User user) {
                    if(user!=null){
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        SharedPreferences preferences = MyApplication.getContext().getSharedPreferences("user",MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("userNmae",user.getLoginId());
                        editor.putString("userPass",user.getLoginPass());
                        editor.commit();
                        startActivity(intent);
                    }else{
                        Toast.makeText(MyApplication.getContext(),"用户名或密码不正确",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
    public void zc(View view){
        Intent intent = new Intent(this,InsertActivity.class);
        startActivity(intent);
    }


}